package cn.iocoder.yudao.module.agent.controller.admin.brokeragewithdrawrecord;

import cn.iocoder.yudao.module.agent.base.MemberRespVO;
import cn.iocoder.yudao.module.agent.controller.admin.withdrawaccount.vo.AgentWithdrawAccountRespVO;
import cn.iocoder.yudao.module.agent.convert.base.AgentConvert;
import cn.iocoder.yudao.module.agent.convert.withdrawaccount.AgentWithdrawAccountConvert;
import cn.iocoder.yudao.module.agent.dal.dataobject.user.AgentUserDO;
import cn.iocoder.yudao.module.agent.dal.dataobject.withdrawaccount.AgentWithdrawAccountDO;
import cn.iocoder.yudao.module.agent.enums.AgentBrokerageWithdrawStatusEnum;
import cn.iocoder.yudao.module.agent.service.withdrawaccount.AgentWithdrawAccountService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.agent.controller.admin.brokeragewithdrawrecord.vo.*;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordDO;
import cn.iocoder.yudao.module.agent.convert.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordConvert;
import cn.iocoder.yudao.module.agent.service.brokeragewithdrawrecord.AgentBrokerageWithdrawRecordService;

@Tag(name = "管理后台 - 代理人佣金提现记录")
@RestController
@RequestMapping("/agent/brokerage-withdraw-record")
@Validated
public class AgentBrokerageWithdrawRecordController {

    @Resource
    private AgentBrokerageWithdrawRecordService brokerageWithdrawRecordService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private AgentWithdrawAccountService withdrawAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建代理人佣金提现记录")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:create')")
    public CommonResult<Long> createBrokerageWithdrawRecord(@Valid @RequestBody AgentBrokerageWithdrawRecordCreateReqVO createReqVO) {
        return success(brokerageWithdrawRecordService.createBrokerageWithdrawRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新代理人佣金提现记录")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:update')")
    public CommonResult<Boolean> updateBrokerageWithdrawRecord(@Valid @RequestBody AgentBrokerageWithdrawRecordUpdateReqVO updateReqVO) {
        brokerageWithdrawRecordService.updateBrokerageWithdrawRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除代理人佣金提现记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:delete')")
    public CommonResult<Boolean> deleteBrokerageWithdrawRecord(@RequestParam("id") Long id) {
        brokerageWithdrawRecordService.deleteBrokerageWithdrawRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得代理人佣金提现记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:query')")
    public CommonResult<AgentBrokerageWithdrawRecordRespVO> getBrokerageWithdrawRecord(@RequestParam("id") Long id) {
        AgentBrokerageWithdrawRecordDO brokerageWithdrawRecord = brokerageWithdrawRecordService.getBrokerageWithdrawRecord(id);

        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(brokerageWithdrawRecord.getUserId());
        MemberRespVO memberRespVO = AgentConvert.INSTANCE.covnertMember(memberUserRespDTO);

        AgentWithdrawAccountDO accountDO = withdrawAccountService.getWithdrawAccountByUserId(brokerageWithdrawRecord.getUserId());
        AgentWithdrawAccountRespVO accountRespVO = AgentWithdrawAccountConvert.INSTANCE.convert(accountDO);

        return success(AgentBrokerageWithdrawRecordConvert.INSTANCE.convert02(brokerageWithdrawRecord, memberRespVO, accountRespVO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得代理人佣金提现记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:query')")
    public CommonResult<List<AgentBrokerageWithdrawRecordRespVO>> getBrokerageWithdrawRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<AgentBrokerageWithdrawRecordDO> list = brokerageWithdrawRecordService.getBrokerageWithdrawRecordList(ids);
        return success(AgentBrokerageWithdrawRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得代理人佣金提现记录分页")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:query')")
    public CommonResult<PageResult<AgentBrokerageWithdrawRecordRespVO>> getBrokerageWithdrawRecordPage(@Valid AgentBrokerageWithdrawRecordPageReqVO pageVO) {
        PageResult<AgentBrokerageWithdrawRecordDO> pageResult = brokerageWithdrawRecordService.getBrokerageWithdrawRecordPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), AgentBrokerageWithdrawRecordDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        List<AgentWithdrawAccountDO> accountList = withdrawAccountService.getWithdrawAccountListByUserIds(memberIds);
        Map<Long, AgentWithdrawAccountDO> accountMap = convertMap(accountList, AgentWithdrawAccountDO::getUserId);

        return success(AgentBrokerageWithdrawRecordConvert.INSTANCE.convertPage02(pageResult, memberMap, accountMap));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出代理人佣金提现记录 Excel")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:export')")
    @OperateLog(type = EXPORT)
    public void exportBrokerageWithdrawRecordExcel(@Valid AgentBrokerageWithdrawRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AgentBrokerageWithdrawRecordDO> list = brokerageWithdrawRecordService.getBrokerageWithdrawRecordList(exportReqVO);
        // 导出 Excel
        List<AgentBrokerageWithdrawRecordExcelVO> datas = AgentBrokerageWithdrawRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "代理人佣金提现记录.xls", "数据", AgentBrokerageWithdrawRecordExcelVO.class, datas);
    }

    @PostMapping("/approve")
    @Operation(summary = "通过申请")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:update')")
    public CommonResult<Boolean> approveBrokerageWithdraw(@Valid @RequestBody AgentBrokerageWithdrawRecordAuditReqVO reqVO) {
        brokerageWithdrawRecordService.auditBrokerageWithdraw(reqVO.getId(), AgentBrokerageWithdrawStatusEnum.AUDIT_SUCCESS, "");
        return success(true);
    }

    @PutMapping("/reject")
    @Operation(summary = "驳回申请")
    @PreAuthorize("@ss.hasPermission('agent:brokerage-withdraw-record:update')")
    public CommonResult<Boolean> rejectBrokerageWithdraw(@Valid @RequestBody AgentBrokerageWithdrawRecordAuditReqVO reqVO) {
        brokerageWithdrawRecordService.auditBrokerageWithdraw(reqVO.getId(), AgentBrokerageWithdrawStatusEnum.AUDIT_FAIL, reqVO.getAuditReason());
        return success(true);
    }

}
